package com.blog.config;
import java.io.IOException;
import java.util.Properties;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;

@Configuration
@ComponentScan(basePackages = {"com.blog.dao"})
public class HibernateConfig {
    @Autowired
    @Qualifier("masterDataSource")
    private DataSource dataSource;

    @Bean(name="sessionFactory")
    public SessionFactory sessionFactory() throws IOException {
        LocalSessionFactoryBean sfb = new LocalSessionFactoryBean();
        sfb.setDataSource(dataSource);
//        sfb.setAnnotatedPackages("com.blog.domain");
        sfb.setPackagesToScan("com.blog.domain");

        Properties hibernateProperties = new Properties();
        hibernateProperties.load(this.getClass().getResourceAsStream("hibernateConfig.properties"));
        sfb.setHibernateProperties(hibernateProperties);
        sfb.afterPropertiesSet();
        return sfb.getObject();
    }
}
